package com.itheima.wms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.wms.controller.dto.GoodsPageDto;
import com.itheima.wms.mapper.GoodsMapper;
import com.itheima.wms.pojo.Goods;
import com.itheima.wms.service.GoodsService;
import com.itheima.wms.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public PageBean<Goods> goodsPage(GoodsPageDto goodsPageDto) {
        PageHelper.startPage(goodsPageDto.getCurrent(), goodsPageDto.getSize());

        Page<Goods> goods = goodsMapper.goodsPage(goodsPageDto);

        int pageSize = goods.getPageSize();

        long total = goods.getTotal();

        int pageNum = goods.getPageNum();

        return new PageBean<>(goods, total, pageSize);
    }

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        Long id = ids.get(0);

        int num = goodsMapper.delete(id);

        if (num > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public Goods getgoodById(Long id) {

        Goods goods = goodsMapper.getgoodById(id);

        return goods;

    }

    @Override
    public void update(Goods goods) {
        goodsMapper.update(goods);
    }
}
